<template>
  <breadcrumb :levelList="levelList" />
  <el-card class="card">
    <el-form v-if="state.showSearch==true" :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item>
        <el-form-item label="部门" prop="dept_id">
          <el-tree-select
            class="depSelect"
            v-model="queryForm.dept_id"
            :data="deptOptions"
            :props="{ value: 'dept_id', label: 'dept_name', children: 'children' }"
            value-key="dept_id"
            placeholder="选择上级部门"
            check-strictly
            clearable
          />
        </el-form-item>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="queryForm.user_name" placeholder="用户名称" clearable />
      </el-form-item>
      <el-form-item label="手机号" prop="queryForm.phonenumber">
         <el-input v-model="queryForm.phonenumber" placeholder="请输入手机号码" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
         <el-button type="primary" plain size="small" @click="addHandle(dataFormRef)" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button type="danger" plain size="small" :disabled="multiple" @click="handleDeleteBatch" >删除</el-button>
      </el-col>
    </el-row>
    <el-table :data="state.dataList" style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="user_id" label="用户编号" />
      <el-table-column prop="user_name" label="账号" width="180" />
      <el-table-column prop="nick_name" label="姓名" width="180" />
      <el-table-column prop="phonenumber" label="电话" width="180" />
      <el-table-column label="状态" align="center" width="100">
       <template #default="scope">
        <el-button v-if="scope.row.status=='0'" size="small" type="primary" plain>正常</el-button>
        <el-button v-if="scope.row.status=='1'" size="small" type="warning" plain>停用</el-button>
       </template>
    </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="resetPass(scope.row)">重置密码</el-button>
          <el-button link size="small" type="primary" @click="setRole(scope.row)">分配角色</el-button>
          <el-button link size="small" @click="editHandle(scope.row)">编辑</el-button>
          <el-button link size="small"  type="danger"  @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination small background layout="prev, pager, next ,total, sizes" :total="state.total" v-model:page-size="state.limit" @change="pageChangeHandle" />
  </el-card>
  <el-dialog :title="state.dialogTitle" v-model="state.showDialog" width="600px" append-to-body>
    <el-form :model="state.dataForm" ref="dataFormRef" :rules="rules"  label-width="120px">
      <el-form-item label="用户账号" prop="user_name">
        <el-input placeholder="请输入用户账号" v-model="dataForm.user_name" />
      </el-form-item>
      <el-form-item label="用户姓名" prop="nick_name">
        <el-input placeholder="请输入用户姓名" v-model="dataForm.nick_name" />
      </el-form-item>
      <el-form-item label="归属部门" prop="deptId">
         <el-tree-select
            v-model="dataForm.dept_id"
            :data="deptOptions"
            :props="{ value: 'dept_id', label: 'dept_name', children: 'children' }"
            value-key="id"
            placeholder="请选择归属部门"
            check-strictly
         />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="dataForm.phonenumber" placeholder="请输入手机号码" maxlength="11" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="请输入邮箱" maxlength="50" />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-radio-group v-model="dataForm.sex">
          <el-radio-button label="男" value="0" />
          <el-radio-button label="女" value="1" />
          <el-radio-button label="未知" value="2" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio-button label="正常" value="0" />
          <el-radio-button label="停用" value="1" />
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit(dataFormRef)">确 定</el-button>
          <el-button @click="cancelHandle">取 消</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog title="重置密码" v-model="showResetPasswordDialog" width="600px" append-to-body>
    <el-form label-width="120px">
      <el-form-item label="新密码" prop="password">
        <el-input placeholder="新密码" v-model="passForm.password" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
          <el-button type="primary" @click="resetpass">确 定</el-button>
          <el-button @click="showResetPasswordDialog=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog title="分配角色" v-model="showSetRoleDialog" width="600px" append-to-body>
    <el-form label-width="120px">
      <el-form-item label="角色组">
        <el-tree
          class="tree-border"
          :data="roleOptions"
          show-checkbox
          ref="roleRef"
          node-key="id"
          empty-text="加载中"
          :props="{ label: 'name', children: 'children' }"
        ></el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
          <el-button type="primary" @click="submitSetRole">确 定</el-button>
          <el-button @click="showSetRoleDialog=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, toRefs, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { CrudTool } from '@/utils/curd'
import { getUserApiUrl, resetPasswordApi, setRoleApi, getUserRoleApi, setUserRoleApi } from '@/api/sys/user'
import { getDeptListApi } from '@/api/sys/dept'
import { getRoleListApi } from '@/api/sys/role'
import { getMenu } from '@/api/sys/menu'
import breadcrumb from '@/views/common/breadcrumb.vue'

const dataFormRef = ref();
const levelList = ['首页','系统设置','用户管理']
const deptOptions = ref([]);
const roleOptions = ref([]);
const roleRef = ref(null);

const showResetPasswordDialog = ref(false)
const passForm = reactive({
  user_id:0,
  password:''
});

const showSetRoleDialog = ref(false)
const roleForm = reactive({
  user_id:0,
  roleIds:[]
});

const state = reactive({
  primaryKey:'user_id',
  queryForm: {
    user_name:'',
    dept_id:'',
    phonenumber:'' 
    
  },
  apiUrl: getUserApiUrl(),
  total:0,
  dataForm:{
    user_id: 0,
    dept_id: undefined,
    user_name: undefined,
    phonenumber:'',
    email:'',
    password:'',
    sex:'1',
    status:'0',

  },
  titleText:['添加用户','修改用户'],
  rules:{
    user_name: [{ required: true, message: "用户名称不能为空", trigger: "blur" }],
    nick_name: [{ required: true, message: "用户名称不能为空", trigger: "blur" }]
  }

});

const { queryForm, dataForm, rules } = toRefs(state);
const { getDataList, addHandle, editHandle, cancelHandle, pageChangeHandle, selectionChangeHandle, handleSubmit, handleDelete, handleDeleteBatch } = CrudTool(state)


const resetPass = (row)=>{
  passForm.user_id = row.user_id;
  passForm.password = '';
  showResetPasswordDialog.value = true
}
const resetpass = ()=>{
  if(passForm.user_id==0){
    return;
  }else if (passForm.password==''){
    return;
  }
  resetPasswordApi(passForm).then(res=>{
    if(res.code==200){
      ElMessage.success(res.msg);
      showResetPasswordDialog.value = false;
    }else{
      ElMessage.error(res.msg);
    }
  })
}

const setRole = (row)=>{
  roleForm.user_id = row.user_id;
  roleForm.roleIds = [];
  showSetRoleDialog.value = true
  roleRef.value.setCheckedKeys([])
  getUserRoleApi(row.user_id).then(res=>{
    roleForm.roleIds = res.data;
    roleRef.value.setCheckedKeys(res.data)
  })

}

const submitSetRole = ()=>{
  roleForm.roleIds = roleRef.value.getCheckedKeys()

  setUserRoleApi(roleForm).then(res=>{
    if(res.code==200){
      ElMessage.success(res.msg);
      showSetRoleDialog.value = false;
    }else{
      ElMessage.error(res.msg);
    }
  })
}

getDataList();

getDeptListApi().then(res => {
  deptOptions.value = res.rows;
});
getRoleListApi().then(res=>{
  roleOptions.value = res.data;
})

</script>

<style scoped>
.card{
  min-height:600px;
}
.tree-border {
  margin-top: 5px;
  border: 1px solid #e5e6e7;
  background: #ffffff none;
  border-radius: 4px;
  width: 100%;
  padding:10px;
}
.depSelect{
  min-width:180px;
}
</style>